package com.google.code.csf.locators.jaxrpc;

import javax.xml.namespace.QName;
import java.net.URL;

public class JBossJaxRpcServiceFactoryServiceLocator<ServiceType> extends JaxRpcServiceFactoryServiceLocator<ServiceType> {
    private static final String CREATE_SERVICE = "createService";
    private URL jaxrpcMappingDocumentUrl;
    private URL securityDocumentUrl;

    public URL getJaxrpcMappingDocumentUrl() {
        return jaxrpcMappingDocumentUrl == null
                ? Thread.currentThread().getContextClassLoader().getResource("META-INF/jaxrpc-mapping.xml")
                : jaxrpcMappingDocumentUrl;
    }

    public void setJaxrpcMappingDocumentUrl(URL jaxrpcMappingDocumentUrl) {
        this.jaxrpcMappingDocumentUrl = jaxrpcMappingDocumentUrl;
    }

    public URL getSecurityDocumentUrl() {
        return securityDocumentUrl;
    }

    public void setSecurityDocumentUrl(URL securityDocumentUrl) {
        this.securityDocumentUrl = securityDocumentUrl;
    }

    @Override
    protected Object createJaxService() throws Throwable {
        Object factory = createJaxRpcServiceFactory();
        return factory.getClass().getMethod(CREATE_SERVICE, URL.class, QName.class, URL.class, URL.class)
                .invoke(factory,
                        getWsdlDocumentUrl(),
                        getQName(),
                        getJaxrpcMappingDocumentUrl(),
                        getSecurityDocumentUrl());
    }

}
